package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.controller.dto.TrainClassDTO;
import com.yqg.education.entity.TeachPlan;
import com.yqg.education.mapper.TeachPlanMapper;
import com.yqg.education.service.ITeachPlanService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TeachPlanController 前端控制器
 *
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/teach-plan")
public class TeachPlanController {
    @Resource
    private TeachPlanMapper teachPlanMapper;
    @Resource
    private ITeachPlanService teachPlanService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody TeachPlan teachPlan) {
        teachPlan.setCreateTime(LocalDateTime.now());
        return Result.success(teachPlanService.saveOrUpdate(teachPlan));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        TeachPlan teachPlan = teachPlanService.getById(id);
        teachPlan.setIsDelete(1);
        return Result.success(teachPlanService.saveOrUpdate(teachPlan));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<TeachPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<TeachPlan> list = teachPlanService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(teachPlanService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name,
                           Integer courseId) {
        pageNum = pageNum * pageSize - pageSize;
        List<TeachPlan> teachPlanList = teachPlanMapper.pageList(pageNum, pageSize, name, courseId);
        Long count = teachPlanMapper.findCount(name, courseId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", teachPlanList);
        map.put("total", count);
        return Result.success(map);
    }

    @GetMapping("/findByCourseId")
    public Result findByCourseId(@RequestParam Integer courseId) {
        LambdaQueryWrapper<TeachPlan> teachPlanQueryWrapper = new LambdaQueryWrapper<>();
        teachPlanQueryWrapper.eq(TeachPlan::getCourseId, courseId);
        teachPlanQueryWrapper.eq(TeachPlan::getIsDelete, 0);
        return Result.success(teachPlanService.list(teachPlanQueryWrapper));
    }

    /**
     * 学员端获取资源信息接口
     */
    @GetMapping("/stuFindResourcePage")
    public Result stuFindResourcePage(@RequestParam Integer pageNum,
                                      @RequestParam Integer pageSize,
                                      @RequestParam(required = false) String name,
                                      @RequestParam(required = false) Integer typeId,
                                      @RequestParam Integer userId,
                                      @RequestParam(required = false) Integer courseId) {
        return Result.success(teachPlanService.findResourcePage(pageNum, pageSize, name, typeId, userId,courseId));
    }
}

